<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit">
    <div class="w-full mt-2 mb-2">
      <div
        v-for="(item, index) in dataRef"
        :key="index"
        class="flex items-center gap-2 mb-2 w-2/3 m-auto"
      >
        <div class="overflow-hidden whitespace-nowrap overflow-ellipsis w-100px text-right">
          {{ item.title }}
        </div>
        <div>
          {{ item.cond }}
        </div>
        <div>
          <Input v-model:value="item.right" />
        </div>
      </div>
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getQueryGetConditionsForScript } from '/@/api/reportForms/reportForms';
  import { Input } from 'ant-design-vue';
  export default defineComponent({
    name: 'SearchTableCondition',
    components: {
      BasicModal,
      Input,
    },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const { createMessage } = useMessage();
      const isUpdate = ref(false);
      const dataRef = ref<Recordable[]>([]);
      const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
        setModalProps({ confirmLoading: false, width: '30%', minHeight: 60 });
        try {
          isUpdate.value = data?.isUpdate;
          const res = await getQueryGetConditionsForScript({
            id: data?.id,
          });
          dataRef.value = res || [];
        } catch (err) {
          console.error(err);
        }
      });

      const getTitle = '条件';

      async function handleSubmit() {
        try {
          if (dataRef.value.length == 0) {
            createMessage.warning('无条件数据');
            return;
          }
          setModalProps({ confirmLoading: true });
          createMessage.success('操作成功');

          const params = {};
          dataRef.value.forEach((item) => {
            params[item.name] = item.right;
          });
          closeModal();
          emit('success', params);
        } catch (err) {
          console.error(err);
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }

      return {
        registerModal,
        getTitle,
        handleSubmit,
        dataRef,
      };
    },
  });
</script>
